package com.yupi.yupicturebackend.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.crypto.digest.DigestUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qcloud.cos.model.PutObjectResult;
import com.yupi.yupicturebackend.annotation.AuthCheck;
import com.yupi.yupicturebackend.common.BaseResponse;
import com.yupi.yupicturebackend.common.ResultUtils;
import com.yupi.yupicturebackend.common.UserHolder;
import com.yupi.yupicturebackend.constant.UserConstant;
import com.yupi.yupicturebackend.exception.BusinessException;
import com.yupi.yupicturebackend.exception.ErrorCode;
import com.yupi.yupicturebackend.exception.ThrowUtils;
import com.yupi.yupicturebackend.manager.CosUtils;
import com.yupi.yupicturebackend.model.dto.UserLoginDTO;
import com.yupi.yupicturebackend.model.dto.UserRegisterDTO;
import com.yupi.yupicturebackend.model.dto.picture.PictureUploadRequest;
import com.yupi.yupicturebackend.model.dto.user.UserAddDTO;
import com.yupi.yupicturebackend.model.dto.user.UserQueryDTO;
import com.yupi.yupicturebackend.model.dto.user.UserUpdateDTO;
import com.yupi.yupicturebackend.model.entity.User;
import com.yupi.yupicturebackend.model.vo.PictureVO;
import com.yupi.yupicturebackend.model.vo.UserVO;
import com.yupi.yupicturebackend.service.PictureService;
import com.yupi.yupicturebackend.service.UserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/file")
public class FileController {
    @Resource
    private PictureService pictureService;

    @PostMapping("/upload")
   // @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    @ApiOperation("文件上传")
    public BaseResponse uploadFile(@RequestPart("file") MultipartFile file, PictureUploadRequest request) {
        UserVO user = UserHolder.getUser();
        User user1 = new User();
        BeanUtil.copyProperties(user,user1);
        PictureVO pictureVO = pictureService.uploadPicture(file, request, user1);
        return ResultUtils.success(pictureVO);
    }
    @PostMapping("/upload")
    // @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    @ApiOperation("通过url上传图片")
    public BaseResponse uploadByUrl(@RequestBody PictureUploadRequest request) {
        UserVO user = UserHolder.getUser();
        User user1 = new User();
        BeanUtil.copyProperties(user,user1);
        String url = request.getUrl();
        PictureVO pictureVO = pictureService.uploadPicture(url, request, user1);
        return ResultUtils.success(pictureVO);
    }
}

